package com.zhou.JavaAndroidFirm.adapter;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Parcelable;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.github.chrisbanes.photoview.OnPhotoTapListener;
import com.github.chrisbanes.photoview.PhotoView;
import com.zhou.JavaAndroidFirm.R;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2018/8/2 0002
 * @des
 */
public class ImageAdapter extends PagerAdapter implements OnPhotoTapListener {
    private List<String> datas = new ArrayList<String>();
    private LayoutInflater inflater;
    private Activity context;

    public void setDatas(List<String> datas) {
        if (datas != null)
            this.datas = datas;
    }

    public ImageAdapter(Activity activity) {
        this.context = activity;
        this.inflater = LayoutInflater.from(context);

    }

    public ImageAdapter(Activity activity, List<String> datas) {
        this.context = activity;
        this.inflater = LayoutInflater.from(context);
        this.datas = datas;

    }

    @Override
    public int getCount() {
        return null == datas ? 0 : datas.size();
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view.equals(object);
    }


    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        FrameLayout view = (FrameLayout) inflater.inflate(R.layout.item_pager_image, container, false);
        if (null != view) {
            final PhotoView imageView = (PhotoView) view.findViewById(R.id.image);
            //loading
            final ProgressBar loading = new ProgressBar(context);
            FrameLayout.LayoutParams loadingLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            loadingLayoutParams.gravity = Gravity.CENTER;
            loading.setLayoutParams(loadingLayoutParams);
            view.addView(loading);
            final String imgurl = datas.get(position);


            //先显示loading
            loading.setVisibility(View.VISIBLE);


            //使用glide监听图片加载事件
            Glide.with(context).setDefaultRequestOptions(
                    new RequestOptions()
                            //全尺寸缓存
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .error(R.drawable.ic_empty_picture))
                    .load(imgurl)
                    //设置缩略图支持,先加载缩略图 然后再加载全图
                    .thumbnail(0.1f)
                    .listener(new RequestListener<Drawable>() {
                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            //加载出现异常
                            loading.setVisibility(View.GONE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            loading.setVisibility(View.GONE);
                            return false;
                        }
                    })
                    .into(imageView);

            //设置监听 单击图片退出
            imageView.setOnPhotoTapListener(this);

            //添加布局
            container.addView(view, 0);

        }
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public void onPhotoTap(ImageView view, float x, float y) {
        context.finish();
    }

    //恢复状态不操作
    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    //保存状态不保存
    @Override
    public Parcelable saveState() {
        return null;
    }
}
